﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FRM_Main
{
    public partial class FRM_SuaNguoiDung : Form
    {
        Quan_Tri_He_ThongEntities5 context = new Quan_Tri_He_ThongEntities5();
        string TenDangNhap;
        NguoiDung nguoidung;
        public FRM_SuaNguoiDung(string pTenDangNhap)
        {
            TenDangNhap = pTenDangNhap;
            InitializeComponent();
        }

        private void FRM_SuaNguoiDung_Load(object sender, EventArgs e)
        {
            cmbPhongBan.DataSource = context.SP_PHONGBAN_SELECT().ToList<PhongBan>();
            cmbPhongBan.DisplayMember = "TenPhongBan";
            cmbPhongBan.ValueMember = "MaPhongBan";

            nguoidung= context.SP_NGUOIDUNG_SELECT_TENDANGNHAP(TenDangNhap).FirstOrDefault<NguoiDung>();
            //nguoidung = (from p in context.NguoiDungs
            //             where p.TenDangNhap == TenDangNhap 
            //             select p).FirstOrDefault<NguoiDung>();
            txtHoTenLot.Text = nguoidung.HoVaTenLot;
            txtTen.Text = nguoidung.Ten;
            txtTenVietTat.Text = nguoidung.TenVietTat;
            cmbGioiTinh.SelectedText = nguoidung.GioiTinh;
            dtNgaySinh.Value = nguoidung.NgaySinh.Value;
            cmbPhongBan.SelectedValue = nguoidung.MaPhongBan;
            txtDTDD.Text = nguoidung.DienThoaiDiDong;
            txtEmail.Text = nguoidung.Email;
            txtTenDangNhap.Text = nguoidung.TenDangNhap;
            txtMatKhau.Text = nguoidung.MatKhau;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int? returnvalue = context.SP_NGUOIDUNG_UPDATE(txtHoTenLot.Text,
                                                     txtTen.Text,
                                                     txtTenVietTat.Text,
                                                     cmbGioiTinh.Text,
                                                     dtNgaySinh.Value,
                                                     Int32.Parse(cmbPhongBan.SelectedValue.ToString()),
                                                     txtDTDD.Text,
                                                     txtEmail.Text,
                                                     txtTenDangNhap.Text,
                                                     txtMatKhau.Text).SingleOrDefault();
            if (returnvalue == 1)
            {
                MessageBox.Show("Sửa người dùng thành công!");
                this.Close();
            }
            else
            {
                MessageBox.Show("Sửa người dùng không thành công");
            }

        
        }
    }
}
